<template>
  <div class="ruler-container" :class="{ 'scale-changed': scaleChanged }">
    <!-- 刻度尺背景 -->
    <div class="ruler-background"></div>
    
    <!-- 画布背景扩展区域 -->
    <div class="canvas-background"></div>
    
    <!-- 垂直刻度尺 -->
    <div class="ruler-vertical" ref="rulerV">
      <div v-for="mm in Math.ceil(Math.max(paperHeight, contentHeight))" :key="`v-${mm}`" 
           class="ruler-mark" 
           :class="{
             'cm-mark': mm % 10 === 0,
             'half-cm-mark': mm % 5 === 0 && mm % 10 !== 0
           }"
           :style="{top: `${mmToPx(mm)}px`}">
        <span v-if="mm % 10 === 0" class="ruler-number">{{mm}}</span>
      </div>
    </div>
    
    <!-- 底部背景填充 -->
    <div class="ruler-v-extension" :style="{ top: `${mmToPx(paperHeight)}px` }"></div>
    
    <!-- 刻度比例显示 -->
    <!-- <div class="ruler-controls">
      <div class="ruler-scale" :class="{ 'scale-changed': scaleChanged }">
        {{ pxPerMm.toFixed(2) }}px/mm
      </div>
    </div> -->
    
    <!-- 画布容器 (插槽) -->
    <div class="canvas-container-wrapper">
      <slot></slot>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, onMounted } from 'vue';

const props = defineProps({
  paperWidth: {
    type: Number,
    default: 148
  },
  paperHeight: {
    type: Number,
    default: 105
  },
  contentWidth: {
    type: Number,
    default: 148
  },
  contentHeight: {
    type: Number,
    default: 105
  },
  pxPerMm: {
    type: Number,
    default: 3.78
  },
  scaleChanged: {
    type: Boolean,
    default: false
  }
});

// 毫米转像素
const mmToPx = (mm) => {
  if (!mm && mm !== 0) return 0;
  return Math.round(mm * props.pxPerMm); // 使用Math.round避免小数点导致的模糊
};

// 监听比例变化
watch(() => props.pxPerMm, (newValue) => {
  console.log('RulerPanel - pxPerMm变化为:', newValue);
}, { immediate: true });

// 监听内容尺寸变化
watch([() => props.contentWidth, () => props.contentHeight], ([width, height]) => {
  console.log('RulerPanel - 内容尺寸变化为:', width, 'x', height);
}, { immediate: true });

// 监听纸张尺寸变化
watch([() => props.paperWidth, () => props.paperHeight], ([width, height]) => {
  console.log('RulerPanel - 纸张尺寸变化为:', width, 'x', height);
}, { immediate: true });

onMounted(() => {
  console.log('RulerPanel组件已挂载');
  console.log('初始尺寸: 纸张', props.paperWidth, 'x', props.paperHeight, 
              '内容', props.contentWidth, 'x', props.contentHeight,
              '比例', props.pxPerMm);
});
</script>

<style lang="scss" scoped>
.ruler-container {
  position: relative;
  background-color: #f8f8f8;
  padding-left: 30px;
  
  /* 刻度尺背景 - 填充整个容器 */
  .ruler-background {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #f8f8f8;
    z-index: 1;
  }
  
  /* 垂直刻度尺 */
  .ruler-vertical {
    position: absolute;
    top: 0;
    left: 0;
    width: 30px;
    background: #f0f0f0;
    border-right: 1px solid #ccc;
    overflow: visible;
    z-index: 10;
    
    .ruler-mark {
      position: absolute;
      height: 1px;
      width: 5px;
      background-color: #666;
      right: 0;
      
      &.cm-mark {
        width: 10px;
        background-color: #333;
      }
      
      &.half-cm-mark {
        width: 7px;
        background-color: #555;
      }
      
      .ruler-number {
        position: absolute;
        right: 12px;
        top: -7px;
        font-size: 10px;
        color: #333;
      }
    }
  }
  
  /* 底部背景填充 */
  .ruler-v-extension {
    position: absolute;
    left: 0;
    width: 30px;
    height: 3000px; /* 延伸足够远 */
    background-color: #f0f0f0;
    border-right: 1px solid #ccc;
    z-index: 9;
  }
  
  /* 画布背景扩展区域 */
  .canvas-background {
    position: absolute;
    top: 0;
    left: 30px;
    right: 0;
    bottom: 0;
    background-color: #f8f8f8;
    z-index: 2;
  }

  .ruler-controls {
    position: absolute;
    right: 15px;
    top: 15px;
    z-index: 100;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    background-color: rgba(255, 255, 255, 0.9);
    padding: 12px;
    width: 100px;
    border-radius: 10px;
    box-shadow: 0 3px 15px 0 rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(5px);
    border: 1px solid rgba(235, 238, 245, 0.9);
    
    .ruler-title {
      font-size: 12px;
      color: #606266;
      margin-bottom: 5px;
      font-weight: bold;
      text-align: center;
      width: 100%;
    }
    
    .ruler-scale {
      font-size: 12px;
      color: #909399;
      margin-top: 5px;
      text-align: center;
      background-color: #f5f7fa;
      padding: 3px 8px;
      border-radius: 4px;
      width: 80%;
      transition: all 0.3s;
      
      &.scale-changed {
        color: #409eff;
        background-color: #ecf5ff;
        transform: scale(1.1);
        font-weight: bold;
      }
    }
  }
}

/* 确保画布容器正确定位 */
.canvas-container-wrapper {
  position: relative;
  z-index: 5;
}
</style> 